﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCF.Hosting;
using System.Drawing;
using Newtonsoft.Json;
using System.Threading;

namespace FrameDetectionTest
{
    class TodoServiceCallback : ITodoServiceCallback
    {
        public static bool IsWork { set; get; } = false;
        public static event SetTextHandle SetSerialNumber;
        public static event RunEventHandle runTest;
        public static event PrintMsgEventHandle Print;
        public static string EQPID { set; get; }
        public static string CurrentEQPID { get; private set; } = string.Empty;
        public static CIM.CIM_TO_STATION cim_to_station = new CIM.CIM_TO_STATION();
        public static CIM.CIM_TO_STATION current_cim_to_station = new CIM.CIM_TO_STATION();
        public void GetDataCallBack(string message)
        {
            try
            {
                if (IsWork) return;
                IsWork = true;
                if (message.Contains("TagName"))
                {
                    cim_to_station = JsonConvert.DeserializeObject<CIM.CIM_TO_STATION>(message);
                    if (cim_to_station.Eqpid == EQPID)
                    {
                        CurrentEQPID = cim_to_station.Eqpid;
                        current_cim_to_station = JsonConvert.DeserializeObject<CIM.CIM_TO_STATION>(message);
                        Log.Print($"CIM to PC: {message}", Log.LogType.cim);
                        SetSerialNumber.Invoke(current_cim_to_station.Sn);
                        Print(message,Color.Gray);
                        runTest();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Print(string.Format("CIM to station message failed: {0}", ex.Message), Log.LogType.error);
            }
            finally
            {
                IsWork = false;
            }
        }

        public void GetDataCallBack2(string message)
        {
            //throw new NotImplementedException();
        }
    }
}
